package com.mini.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * DES :
 * 上传文件到Vod的工具类
 */
@Component
@Slf4j
public class UploadQcloudVodUtil {

    @Value("${qcloudossappId}")
    long qcloudossappId;

    @Value("${qcloudosssecretId}")
    String qcloudosssecretId;

    @Value("${qcloudosssecretKey}")
    String qcloudosssecretKey;

    @Value("${qcloudossregin}")
    String qcloudossregin;

    @Value("${qcloudosshost}")
    String qcloudossDomain;

    public VodUploadResponse uploadVodFile(File file,String suffix) {
        VodUploadClient client = new VodUploadClient(qcloudosssecretId, qcloudosssecretKey);
        VodUploadRequest request = new VodUploadRequest();
        request.setMediaFilePath(file.getPath());
        request.setConcurrentUploadNumber(5);
        request.setMediaName(file.getName());
//        request.setSubAppId(qcloudossappId);
        try {
            VodUploadResponse response = client.upload(qcloudossregin, request);
            if(response != null){
                String murl = response.getMediaUrl();
                String i = murl.substring(7);
                String  n = i.substring(i.indexOf("/"));
                response.setMediaUrl(qcloudossDomain+n);
            }
            log.info("Upload Vod response={}", JSONObject.toJSONString(response));
            return response;
        } catch (Exception e) {
            // 业务方进行异常处理
            log.error("Upload Vod Err", e);
        }
        return null;
    }

  /*  public static void main(String[] args) {
        String s ="http://1257236698.vod2.myqcloud.com/f5aa9d0bvodbj1257236698/10ed87455285890807447996957/f0.mp4";
        String i = s.substring(7);
        String  n = i.substring(i.indexOf("/"));
        System.out.printf(n+"");
    }*/

    /**
     * 下发转码任务
     * @param fileId
     * @param definition 100210
     * @return
     */
    public ProcessMediaResponse transcodeTask(String fileId,Long definition,Long coverModelId){
        try{

            Credential cred = new Credential(qcloudosssecretId, qcloudosssecretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("vod.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            VodClient client = new VodClient(cred, "", clientProfile);

            ProcessMediaRequest req = new ProcessMediaRequest();
            req.setFileId(fileId);
            MediaProcessTaskInput mediaProcessTaskInput1 = new MediaProcessTaskInput();

            TranscodeTaskInput[] transcodeTaskInputs1 = new TranscodeTaskInput[1];
            TranscodeTaskInput transcodeTaskInput1 = new TranscodeTaskInput();
            transcodeTaskInput1.setDefinition(definition);
            transcodeTaskInputs1[0] = transcodeTaskInput1;

            mediaProcessTaskInput1.setTranscodeTaskSet(transcodeTaskInputs1);


            CoverBySnapshotTaskInput[] coverBySnapshotTaskInputs1 = new CoverBySnapshotTaskInput[1];
            CoverBySnapshotTaskInput coverBySnapshotTaskInput1 = new CoverBySnapshotTaskInput();
            coverBySnapshotTaskInput1.setDefinition(coverModelId);
            coverBySnapshotTaskInput1.setPositionType("Time");
            coverBySnapshotTaskInput1.setPositionValue(0F);
            coverBySnapshotTaskInputs1[0] = coverBySnapshotTaskInput1;

            mediaProcessTaskInput1.setCoverBySnapshotTaskSet(coverBySnapshotTaskInputs1);

            req.setMediaProcessTask(mediaProcessTaskInput1);


            ProcessMediaResponse resp = client.ProcessMedia(req);

            log.info(ProcessMediaResponse.toJsonString(resp));
            return resp;
        } catch (TencentCloudSDKException e) {
            log.error("转码失败fileId={}",fileId,e);
        }
        return null;

    }

    /**
     * 查询视频信息
     * @param fileId
     * @return
     */
    public DescribeMediaInfosResponse getVodInfo(String fileId){
        try{

            Credential cred = new Credential(qcloudosssecretId,qcloudosssecretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("vod.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            VodClient client = new VodClient(cred, "", clientProfile);

//            String params = "{\"FileIds\":[\"5285890807447998311\"]}";
            Map<String,Object> p = new HashMap<>();
            List fileIds = new ArrayList<>();
            fileIds.add(fileId);
            p.put("FileIds",fileIds);
            log.info(JSONObject.toJSONString(p));
            DescribeMediaInfosRequest req = DescribeMediaInfosRequest.fromJsonString(JSONObject.toJSONString(p), DescribeMediaInfosRequest.class);

            DescribeMediaInfosResponse resp = client.DescribeMediaInfos(req);

            log.info(DescribeMediaInfosResponse.toJsonString(resp));
            return resp;
        } catch (TencentCloudSDKException e) {
            log.error("查询视频信息fileId={}",fileId,e);
        }
        return null;
    }


}

